﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_SystemCycle_SystemCycleManager : System.Web.UI.Page
{
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    Authentication dllAuthentication = new Authentication();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isAdminLogin();
        this.lbtnAddMonthDate.Enabled = false;
        this.lbtnAddQuarterDate.Enabled = false;
        this.lbtnAddWeekDate.Enabled = false;
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "系统周期定义");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.lbtnAddMonthDate.Enabled = true;
                this.lbtnAddQuarterDate.Enabled = true;
                this.lbtnAddWeekDate.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {

            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
            }
        } 
        if (!IsPostBack)
        {
            //绑定所有的年份
            bindDropDownListYear(this.drpYear1);
            bindDropDownListYear(this.drpYear2);
            bindDropDownListYear(this.drpYear3);

            //根据年绑定年周期
            bindDropDownListQuarter(this.drpYear2, this.drpQuarter1);
            bindDropDownListQuarter(this.drpYear3,this.drpQuarter2);

            //根据年绑定月周期
            bindDropDownListMonth(this.drpQuarter2,this.drpMonth1,this.drpYear3);
            //绑定年周期GridView
            bindGridViewQuarter(this.gdvQuarter, this.drpYear1);
            //绑定月周期GridView
            bindGridViewMonth(this.gdvMonth, this.drpQuarter1);
            //绑定周周期GridView
            bindGridViewWeek(this.gdvWeek, this.drpMonth1);
        }
    }


    /// <summary>
    /// DropDownList更改索引方法（通用）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-14</date>
    protected void dropDownListSelectIndex(object sender, EventArgs e)
    {
        //第一个年份索引改变
        if(sender.Equals(this.drpYear1))
        {
            bindGridViewQuarter(this.gdvQuarter, this.drpYear1);
        }
        //第二个年份索引改变
        if(sender.Equals(this.drpYear2))
        {
            bindDropDownListQuarter(this.drpYear2, this.drpQuarter1);
            bindGridViewMonth(this.gdvMonth, this.drpQuarter1);
        }
        //第三个年份索引改变
        if(sender.Equals(this.drpYear3))
        {
            bindDropDownListQuarter(this.drpYear3, this.drpQuarter2,this.drpMonth1);
            bindGridViewWeek(this.gdvWeek, this.drpYear3);
        }
        //第一个年索引改变
        if(sender.Equals(this.drpQuarter1))
        {           
            bindGridViewMonth(this.gdvMonth, this.drpQuarter1);
        }
        //第二个年索引改变
        if (sender.Equals(this.drpQuarter2))
        {
            bindDropDownListMonth(this.drpQuarter2, this.drpMonth1,this.drpYear3);
            bindGridViewWeek(this.gdvWeek, this.drpMonth1);
        }
        //第三个月索引改变
        if (sender.Equals(this.drpMonth1))
        {
            bindGridViewWeek(this.gdvWeek, this.drpMonth1);
        }


    }

    #region 绑定DropDownList
    /// <summary>
    /// 绑定年份DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-12</date>
    void bindDropDownListYear(DropDownList _drp)
    {
        for (int i = 2009; i <= 2100; i++)
        {
            ListItem li = new ListItem(i.ToString());
            li.Value = i.ToString();
            _drp.Items.Add(li);
        }
    }

    /// <summary>
    /// 根据年份绑定年周期DropDownList
    /// </summary>
    /// <param name="_drpYear">年份DropDownList</param>
    /// <param name="_drpQuarter">年周期DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-12</date>
    void bindDropDownListQuarter(DropDownList _drpYear, DropDownList _drpQuarter)
    {
        object[] _value = new object[1];
        _value[0] = _drpYear.SelectedValue;
        DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
        if (_quarter.Rows.Count != 0)
        {
            _drpQuarter.DataSource = _quarter;
            _drpQuarter.DataTextField = "QuarterName";
            _drpQuarter.DataValueField = "Quarter_Id";
            _drpQuarter.DataBind();
        }
        else
        {
            ListItem li = new ListItem("无年周期");
            _drpQuarter.Items.Clear();
            _drpQuarter.Items.Add(li);
        }
    }


    /// <summary>
    /// 根据年份绑定年周期DropDownList，同时根据年绑定月周期
    /// </summary>
    /// <param name="_drpYear">年份DropDownList</param>
    /// <param name="_drpQuarter">年周期DropDownList</param>
    /// <param name="_drpMonth">月周期DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-12</date>
    void bindDropDownListQuarter(DropDownList _drpYear, DropDownList _drpQuarter,DropDownList _drpMonth)
    {
        //绑定年周期
        object[] _value1 = new object[1];
        _value1[0] = _drpYear.SelectedValue;
        DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value1);
        if (_quarter.Rows.Count != 0)
        {
            _drpQuarter.DataSource = _quarter;
            _drpQuarter.DataTextField = "QuarterName";
            _drpQuarter.DataValueField = "Quarter_Id";
            _drpQuarter.DataBind();
        }
        else
        {
            ListItem li = new ListItem("无年周期");
            _drpQuarter.Items.Clear();
            _drpQuarter.Items.Add(li);
        }
        //绑定月周期
        object[] _value2 = new object[2];
        _value2[0] = _drpYear.SelectedValue;
        _value2[1] = _drpQuarter.SelectedItem.Text;
        DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value2);
        if (_month.Rows.Count != 0)
        {
            _drpMonth.DataSource = _month;
            _drpMonth.DataTextField = "MonthName";
            _drpMonth.DataValueField = "Month_Id";
            _drpMonth.DataBind();
            
        }
        else
        {
            ListItem li = new ListItem("无月周期");
            _drpMonth.Items.Clear();
            _drpMonth.Items.Add(li);
        }
    }



    /// <summary>
    /// 根据年绑定月周期DropDownList
    /// </summary>
    /// <param name="_drpYear">年DropDownList</param>
    /// <param name="_drpQuarter">年周期DropDownList</param>
    /// <param name="_drpMonth">月周期DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-12</date>
    void bindDropDownListMonth(DropDownList _drpQuarter, DropDownList _drpMonth, DropDownList _drpYear)
    {
        object[] _value = new object[2];
        _value[0] = _drpYear.SelectedValue;
        _value[1] = _drpQuarter.SelectedItem.Text;
        DataTable _quarter = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
        if (_quarter.Rows.Count != 0)
        {
            _drpMonth.DataSource = _quarter;
            _drpMonth.DataTextField = "MonthName";
            _drpMonth.DataValueField = "Month_Id";
            _drpMonth.DataBind();
        }
        else
        {
            ListItem li = new ListItem("无月周期");
            _drpMonth.Items.Clear();
            _drpMonth.Items.Add(li);
        }
    }

    #endregion


    #region 绑定GridView

    /// <summary>
    /// 根据年份绑定年周期
    /// </summary>
    /// <param name="_gdvQuarter">要绑定的年GridView</param>
    /// <param name="_drpYear">获得年份的DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    void bindGridViewQuarter(GridView _gdvQuarter,DropDownList _drpYear)
    {
        object[] _value = new object[1];
        _value[0] = _drpYear.SelectedValue;
        DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
        if (_quarter.Rows.Count != 0)
        {
            _gdvQuarter.DataSource = _quarter;            
            _gdvQuarter.DataBind();            
        }
        else
        {
            DataTable _empty = new DataTable();
            _gdvQuarter.DataSource = _empty;
            _gdvQuarter.DataBind();
        }
    }

    /// <summary>
    /// 根据年绑定月周期
    /// </summary>
    /// <param name="_gdvQuarter">要绑定的年GridView</param>
    /// <param name="_drpYear">获得年份的DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    void bindGridViewMonth(GridView _gdvMonth, DropDownList _drpQuarter)
    {
        object[] _value = new object[2];
        _value[0] = this.drpYear2.SelectedValue; 
        _value[1] = _drpQuarter.SelectedItem.Text;
        DataTable _Month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
        if (_Month.Rows.Count != 0)
        {
            _gdvMonth.DataSource = _Month;
            _gdvMonth.DataBind();
            
        }
        else
        {
            DataTable _empty = new DataTable();
            _gdvMonth.DataSource = _empty;
            _gdvMonth.DataBind();
        }
    }


    /// <summary>
    /// 根据月绑定周周期
    /// </summary>
    /// <param name="_gdvQuarter">要绑定的周GridView</param>
    /// <param name="_drpYear">获得月的DropDownList</param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    void bindGridViewWeek(GridView _gdvWeek, DropDownList _drpMonth)
    {
        object[] _value = new object[3];
        _value[0] = this.drpYear3.SelectedValue;
        _value[1] = this.drpQuarter2.SelectedItem.Text;
        _value[2] = _drpMonth.SelectedItem.Text;
        DataTable _Week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
        if (_Week.Rows.Count != 0)
        {
            _gdvWeek.DataSource = _Week;
            _gdvWeek.DataBind();
            
        }
        else
        {
            DataTable _empty = new DataTable();
            _gdvWeek.DataSource = _empty;
            _gdvWeek.DataBind();
        }
    }

    #endregion


    #region 删除按钮事件

    /// <summary>
    /// 删除年周期按钮事件(只有年份的)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    protected void btnQuarterDelete_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvQuarter.DataKeys[i]["Quarter_Id"].ToString());
        int result = dllQuarterDateManager.del(QuarterDateManager.field.Quarter_Id.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除年周期未成功，请重试！");
        }
        else
        {
            Response.Redirect("SystemCycleManager.aspx");
        }
    }


    /// <summary>
    /// 删除月周期按钮事件(有年份、年的)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    protected void btnMonthDelete_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvMonth.DataKeys[i]["Month_Id"].ToString());
        int result = dllMonthDateManager.del(MonthDateManager.field.Month_Id.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除月周期未成功，请重试！");
        }
        else
        {
            Response.Redirect("SystemCycleManager.aspx");
        }
    }

    /// <summary>
    /// 删除周周期按钮事件(有年份、年、周的)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-13</date>
    protected void btnWeekDelete_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.gdvWeek.DataKeys[i]["Week_Id"].ToString());
        int result = dllWeekDateManager.del(WeekDateManager.field.Week_Id.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("删除周周期未成功，请重试！");
        }
        else
        {
            Response.Redirect("SystemCycleManager.aspx");
        }
    }

    #endregion



    /// <summary>
    /// 添加年周期按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnAddQuarterDate_Click(object sender, EventArgs e)
    {
        ListItem _year = this.drpYear1.SelectedItem;
        if (_year != null)
        {
            Utility.HttpHelp.redirectPage("QuarterDate_Add.aspx?Year="+_year.Text);
        }
      
    }

    /// <summary>
    /// 月周期添加事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <Date>2009-2-26</Date>
    protected void lbtnAddMonthDate_Click(object sender, EventArgs e)
    {
        ListItem _year = this.drpYear2.SelectedItem;
        ListItem _quarter = this.drpQuarter1.SelectedItem;
        if (_year != null&&_quarter!=null)
        {
            Utility.HttpHelp.redirectPage("MonthDate_Add.aspx?Year=" + _year.Text+"&Quarter="+_quarter.Value);
        }
    }


    /// <summary>
    /// 周周期添加事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-26</date>
    protected void lbtnAddWeekDate_Click(object sender, EventArgs e)
    {
        ListItem _year = this.drpYear3.SelectedItem;
        ListItem _quarter = this.drpQuarter2.SelectedItem;
        ListItem _month = this.drpMonth1.SelectedItem;
        if (_year != null && _quarter != null&&_month!=null)
        {
            Utility.HttpHelp.redirectPage("WeekDate_Add.aspx?Year=" + _year.Text + "&Quarter=" + _quarter.Value+"&Month="+_month.Value);
        }
    }


    /// <summary>
    /// 年周期修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyQuarter_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        string _url = "QuarterDate_Modify.aspx?Quarter_Id=" + this.gdvQuarter.DataKeys[i]["Quarter_Id"].ToString();
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=250,width=530,top=200,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }


    /// <summary>
    /// 月周期修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyMonth_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        string _url = "MonthDate_Modify.aspx?Month_Id=" + this.gdvMonth.DataKeys[i]["Month_Id"].ToString();
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=250,width=530,top=200,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }


    /// <summary>
    /// 周周期修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyWeek_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        string _url = "WeekDate_Modify.aspx?Week_Id=" + this.gdvWeek.DataKeys[i]["Week_Id"].ToString();
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=250,width=530,top=200,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }
}
